<template>
  <div class="kehu">
    <div class="header t-right">
      <el-button type="primary"
                 @click="dialogVisible=true">添加子管理员</el-button>
    </div>
    <el-table :data="keHuList"
              v-loading="loading"
              style="width: 100%">
      <el-table-column prop="userName"
                       label="用户姓名"
                       width="100">
      </el-table-column>
      <el-table-column prop="createTime"
                       label="创建时间"
                       width="180">
      </el-table-column>
      <el-table-column prop="nickName"
                       width="100"
                       label="昵称">
      </el-table-column>
      <el-table-column prop="mobile"
                       label="手机号">
      </el-table-column>

      <el-table-column label="账户类型">
        <template slot-scope="scope">
          <span>{{scope.row.isAdmin==1?'子管理员':'客户'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="160">
        <template slot-scope="scope">
          <el-button size="mini"
                     plain
                     type="primary"
                     @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="total,prev, pager, next"
                   @current-change="handleCurrentChange"
                   background
                   :total="total">
    </el-pagination>

    <!-- 添加客户表单 -->
    <el-dialog title="添加客户"
               :close-on-click-modal="false"
               :visible.sync="dialogVisible"
               width="500">
      <el-form :model="addKeHuForm"
               :rules="rules"
               ref="addKeHuForm"
               label-width="100px">
        <el-form-item label="账号"
                      prop="userName">
          <el-input v-model="addKeHuForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="手机号"
                      prop="mobile">
          <el-input v-model="addKeHuForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password">
          <el-input type="password"
                    v-model="addKeHuForm.password"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onAddSubAdmin">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改客户基础信息 -->
    <el-dialog title="修改客户基础信息"
               :close-on-click-modal="false"
               :visible.sync="editDialog"
               width="500">
      <el-form :model="editKeHuForm"
               :rules="editRules"
               ref="editKeHuForm"
               label-width="100px">
        <el-form-item label="手机号"
                      prop="mobile">
          <el-input v-model="editKeHuForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="昵称"
                      prop="nickName">
          <el-input v-model="editKeHuForm.nickName"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onEditSubAdmin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, deleteUsers, addSubAdmin, editUserInfo } from '@/services/services_user'

export default {
  name: "keHu",
  data () {
    return {
      loading: false,
      keHuList: [],
      current: 1, //页码
      size: 10,//条数
      total: 0,
      dialogVisible: false,
      editDialog: false,
      // form
      addKeHuForm: {
        mobile: '',
        password: '',
        userName: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/, message: '请输入正确的手机号码' }
        ],
        password: [
          { required: true, message: '请输入账户密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      },
      editKeHuForm: {
        mobile: '',
        nickName: '',
        id: ''
      },
      editRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/, message: '请输入正确的手机号码' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getUserListAll (data) {
      this.loading = true
      const res = await getUserList(data)
      if (res.code == 0) {
        this.keHuList = res.data.records
        this.total = res.data.total
        this.loading = false
      } else {
        this.$message.warning(res.msg)
        this.loading = false
      }
    },
    handleCurrentChange (val) {
      this.current = val
      this.getUserListAll({ current: this.current, size: this.size })
    },
    // 删除
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUsers({ ids: [row.id] }).then(res => {
          if (res.code == 0) {
            this.getUserListAll({ current: this.current, size: this.size })
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 修改
    handleEdit (index, row) {
      const { nickName, mobile, id } = row
      this.editKeHuForm = {
        nickName: nickName,
        mobile: mobile,
        id: id
      }
      this.editDialog = true
    },
    // 添加子管理员
    async onAddSubAdmin () {
      const data = { ...this.addKeHuForm }
      data.password = window.btoa(data.password)
      const res = await addSubAdmin(data)
      if (res.code == 0) {
        this.$message.success('添加成功')
        this.addKeHuForm = {
          mobile: '',
          password: '',
          userName: ''
        }
        this.dialogVisible = false
        this.getUserListAll({ current: this.current, size: this.size })
      }
    },
    // 修改用户基础信息
    async onEditSubAdmin () {
      const data = { ...this.editKeHuForm }
      const res = await editUserInfo(data)
      if (res.code == 0) {
        this.$message.success('修改成功')
        this.editKeHuForm = {
          mobile: '',
          nickName: '',
          id: ''
        }
        this.editDialog = false
        this.getUserListAll({ current: this.current, size: this.size })
      }
    }
  },
  created () {
    this.getUserListAll({ current: this.current, size: this.size })
  }
}
</script>

<style>
</style>